package com.dangf.app.launcher.dialog

import android.view.View
import android.widget.Button
import android.widget.EditText
import com.dangf.app.launcher.R
import com.dangf.app.launcher.app.SP
import pw.hais.utils_lib.app.AppBaseActivity
import pw.hais.utils_lib.app.BaseDialog
import pw.hais.utils_lib.utils.L
import pw.hais.utils_lib.utils.SPUtils

/**
 * 挂机锁界面
 * Created by 管理员 on 2017/9/12.
 */
class OutboardMotorDialog(context: AppBaseActivity) : BaseDialog(context, R.layout.windows_outbard_motor,false), View.OnClickListener {
    private var button_clean: Button? = null
    private var button_onhook: Button? = null
    private var edit_outbard_password: EditText? = null
    private var edit_outbard_message: EditText? = null

    override fun initView(v: View) {
        edit_outbard_password = v.findViewById(R.id.edit_outbard_password)
        edit_outbard_message = v.findViewById(R.id.edit_outbard_message)
        button_clean = v.findViewById(R.id.button_clean)
        button_onhook = v.findViewById(R.id.button_onhook)
        button_clean?.setOnClickListener(this)
        button_onhook?.setOnClickListener(this)
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.button_clean -> dismiss()
            R.id.button_onhook -> {
                val password = edit_outbard_password?.text.toString()
                val message = edit_outbard_message?.text.toString()
                if (password.isEmpty() || message.isEmpty()) L.showShort("请设置您的挂机密码和提示语后进行挂机！")
                else if (button_onhook?.text == "挂机") {
                    L.showLong("您已开始挂机，请您牢记您设置的挂机密码！")
                    button_onhook?.text = "解锁"
                    edit_outbard_password?.text?.clear()
                    edit_outbard_message?.isEnabled = false
                    button_clean?.visibility = View.GONE
                    SPUtils.saveObject(SP.SP_OUTHOARD_PASSWORD, password)
                } else if (button_onhook?.text == "解锁") {
                    if (password != SPUtils.getObject(SP.SP_OUTHOARD_PASSWORD)) L.showShort("解锁密码错误，解锁失败！")
                    else {
                        SPUtils.delObject(SP.SP_OUTHOARD_PASSWORD)
                        L.showShort("解锁成功！")
                        dismiss()
                    }
                }
            }
        }
    }
}